package test2.spider.news.cnfol;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

/**
 * 中金理财新闻抓取
 * @author Administrator
 *
 */
public class Cnfol {
	
	static String[] channels = new String[]{"licaizixun", "licaigonglue", "caishenghuo", "shoucangjingcui"};
	
	static String link = "http://money.cnfol.com/xxx/";
	static String link_prefix = "http://money.cnfol.com/xxx/index_";
	static String link_suffix = ".shtml";
	static String url = "jdbc:mysql://localhost/demo?characterEncoding=UTF-8";
	static String username = "root";
	static String password = "";
	public static ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
	///index_02.shtml

	static {
		DruidPlugin dp = new DruidPlugin(url, username, password);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		arp.setDevMode(true);
		arp.setShowSql(true);
		dp.start();
		arp.start();
	}
	
	public static void main(String[] args) {
		long l1 = System.currentTimeMillis();
		
		for(String channel : channels) {
			new MainProcessor().process(link.replaceFirst("xxx", channel));
			int index = 2;
			while(true) {
				String page = (index++) + "";
				int len = page.length();
				if(len==1)
					page = "0" + page;
				try {
					new MainProcessor().process(link_prefix.replaceFirst("xxx", channel) + page + link_suffix);
				} catch (Exception e) {
					e.printStackTrace();
					if(e.getMessage().equals("the end")) {
						break;
					}
				}
			}
		}
		
		threadPool.shutdown();
		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				long l2 = System.currentTimeMillis();
				System.out.println("共耗时：" + (l2 - l1)/1000 + "s");
			}
		});
	}

}
